<template>
  <div class="sso-login-container">
    <!-- 背景装饰 -->
    <div class="bg-decoration"></div>

    <!-- 登录卡片 -->
    <div class="login-card">
      <div class="card-header">
        <h2 class="title">统一认证中心</h2>
        <p class="subtitle">请登录以访问您的账户</p>
      </div>

      <div class="card-body">
        <!-- 登录表单 -->
        <form class="login-form">
          <!-- 用户名输入 -->
          <div class="form-group">
            <div class="input-container">
              <i class="fa fa-user input-icon"></i>
              <input
                v-model="username"
                type="text"
                placeholder="用户名"
                class="form-input"
                @input="validateField('username')"
              />
            </div>
            <div class="error-message" v-if="validation.username">
              {{ validation.username }}
            </div>
          </div>

          <!-- 密码输入 -->
          <div class="form-group">
            <div class="input-container">
              <i class="fa fa-lock input-icon"></i>
              <input
                v-model="password"
                type="password"
                placeholder="密码"
                class="form-input"
                @input="validateField('password')"
              />
            </div>
            <div class="error-message" v-if="validation.password">
              {{ validation.password }}
            </div>
          </div>

          <!-- 登录按钮 -->
          <button
            :style="!isFormValid ? { opacity: 0.4, cursor: 'not-allowed' } : {}"
            :disabled="!isFormValid"
            @click="loginHandler"
            type="button"
            class="login-button"
          >
            登录
            <i class="fa fa-arrow-right button-icon"></i>
          </button>
        </form>
      </div>

      <div class="card-footer">
        <p class="register-text">
          还没有账户?
          <a @click="goRegisterHandler" class="register-link">立即注册</a>
        </p>
      </div>
    </div>

    <!-- 页脚 -->
    <footer class="login-footer">
      <p class="footer-text">© 2025 统一认证中心 版权所有</p>
    </footer>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      validation: {
        username: '',
        password: '',
      },
    }
  },
  computed: {
    // 计算属性：表单值是否有效
    isFormValid() {
      return this.username.trim() && this.password.trim()
    },
  },
  methods: {
    loginHandler() {
      this.axios
        .post('/login', { username: this.username, password: this.password })
        .then((res) => {
          if (res.code == 2 || res.code == 1) {
            this.$message.error('账号或密码错误')
          } else if (res.code == 0) {
            this.$message.success('登录成功')
            if (res.data.token) {
              localStorage.setItem('token', res.data.token)
            }
            if (this.$route.query.redirect_url) {
              window.location.href = this.$route.query.redirect_url
            }
            this.$router.push('/')
          }
        })
    },
    goRegisterHandler() {
      this.$router.push('/register')
    },
    validateField(field) {
      if (field === 'username') {
        this.validation.username = this.username.trim() ? '' : '请输入用户名'
      } else if (field === 'password') {
        this.validation.password = this.password.trim() ? '' : '请输入密码'
      }
    },
  },
}
</script>

<style scoped>
/* 全局样式 */
.sso-login-container {
  font-family: 'Helvetica Neue', Arial, sans-serif;
  margin: 0;
  padding: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #4a6fe3 0%, #7928ca 100%);
  position: relative;
  overflow: hidden;
}

/* 背景装饰 */
.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('/login-bg.jpg') center/cover no-repeat;
  opacity: 0.1;
  z-index: 0;
}

/* 登录卡片 */
.login-card {
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  width: 400px;
  max-width: 90%;
  z-index: 1;
  overflow: hidden;
}

.card-header {
  text-align: center;
  padding: 40px 30px 20px;
}

.logo {
  height: 50px;
  margin-bottom: 15px;
}

.title {
  font-size: 24px;
  font-weight: 500;
  color: #333;
  margin-bottom: 10px;
}

.subtitle {
  font-size: 14px;
  color: #666;
}

.card-body {
  padding: 20px 30px 30px;
}

/* 登录表单 */
.login-form {
  display: flex;
  flex-direction: column;
}

.form-group {
  margin-bottom: 20px;
}

.input-container {
  display: flex;
  align-items: center;
  border: 1px solid #ddd;
  border-radius: 5px;
  padding: 10px 15px;
  transition: border-color 0.3s;
}

.input-container:hover {
  border-color: #6c63ff;
}

.input-icon {
  color: #999;
  margin-right: 10px;
  font-size: 16px;
}

.form-input {
  flex: 1;
  border: none;
  outline: none;
  font-size: 16px;
  color: #333;
}

.form-input::placeholder {
  color: #999;
}

.login-button {
  background: linear-gradient(135deg, #6c63ff 0%, #8e2de2 100%);
  color: white;
  border: none;
  border-radius: 5px;
  padding: 12px 20px;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.3s, transform 0.3s;
}

.login-button:hover {
  background: linear-gradient(135deg, #5a52e0 0%, #7a1dd1 100%);
  transform: translateY(-2px);
}

.button-icon {
  margin-left: 10px;
}

.error-message {
  color: #e53e3e;
  font-size: 14px;
  margin-top: 5px;
  animation: fadeIn 0.3s ease;
}

/* 其他选项 */
.other-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  font-size: 14px;
}

.remember-me {
  display: flex;
  align-items: center;
}

.remember-checkbox {
  margin-right: 5px;
}

.remember-label {
  color: #666;
  cursor: pointer;
}

.forgot-password {
  color: #6c63ff;
  text-decoration: none;
  transition: color 0.3s;
}

.forgot-password:hover {
  color: #8e2de2;
}

/* 卡片底部 */
.card-footer {
  background-color: #f8f9fa;
  padding: 20px 30px;
  text-align: center;
  border-top: 1px solid #eee;
}

.register-text {
  font-size: 14px;
  color: #666;
}

.register-link {
  color: #6c63ff;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s;
  cursor: pointer;
}

.register-link:hover {
  color: #8e2de2;
}

/* 页脚 */
.login-footer {
  margin-top: 30px;
  z-index: 1;
}

.footer-text {
  color: white;
  font-size: 14px;
  opacity: 0.7;
}
</style>